package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.UserService;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        try{
            User user =
                    (User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/getMenuData")
    public Result getMenuData(String username){
        try{
            List<Menu> menuList = userService.getMenuData(username);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }

    }
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            userService.add(user, roleIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = userService.findPage(queryPageBean);
        return page;
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list = userService.findAll();
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,list);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            userService.edit(user, roleIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    @RequestMapping("/findroleIdsByUserId")
    public Result findroleIdsByUserId(Integer id){
        try {
            Integer[] ids = userService.findroleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,ids);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }
}
